package com.fg.cloud.system.controller.order;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.vo.FactoryUserVo;
import com.fg.cloud.manager.entry.order.OrderDeliveryInfoEntity;
import com.fg.cloud.manager.entry.order.vo.OrderDeliveryInfoVo;
import com.fg.cloud.manager.service.order.OrderDeliveryService;

/**
 * 系统平台发货单
 * chenyl
 */
@Controller
@RequestMapping("SystemOrderDelivery")
public class SystemOrderDelivery extends BaseController{

	@Autowired
	private OrderDeliveryService orderDeliveryService;
	
	/**
	 * 跳转到发货单首页
	 * */
	@RequestMapping("index")
	private String Index(){
		return "order/orderDelivery";
	}
	
	/**
	 * 
	 * Description:高级查询
	 * @param oe
	 * @author 陈银龙
	 * @date 2017年8月28日上午9:28:06
	 */
	@RequestMapping(value = "/findBySenior", method = RequestMethod.POST)
	@ResponseBody
	public Map<String ,Object> findBySenior(HttpServletRequest request, HttpServletResponse response, OrderDeliveryInfoVo ov){
		Map<String ,Object> usmap = new HashMap<String, Object>();
		try{
			if(ov.getRows() == null || ov.getRows() < 0 || ov.getPage() == null || ov.getPage() < 0){
				usmap.put(String.valueOf(StatusCode.LACKVALUE), ResultType.INPUT_NULL);
				return usmap;
			}
			PageUtils<OrderDeliveryInfoEntity> pageinfo = new PageUtils<OrderDeliveryInfoEntity>(ov.getPage(), ov.getRows(),ov.getSort(),ov.getOrder());
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("beginDate", ov.getBeginDate());
			condition.put("endDate", ov.getEndDate());
			condition.put("productInfo", ov.getProductInfo());
			condition.put("receiveInfo", ov.getReceiveInfo());
			condition.put("ageName", ov.getAgeName());
			condition.put("ageLevelId", ov.getAgeLevelId());
			condition.put("areaId", ov.getAreaId());
			condition.put("areaNumber", ov.getAreaNumber());
			condition.put("facId", ov.getFacId());
			condition.put("sourceBillNo", ov.getSourceBillNo());
			condition.put("deliveryNo", ov.getDeliveryNo());
			condition.put("deliveryStatus", ov.getDeliveryStatus());
			condition.put("keyword", ov.getKeyword());

			orderDeliveryService.findBySenior(pageinfo);
			usmap.put("total", pageinfo.getTotal());
			usmap.put("rows", pageinfo.getData());
			return usmap;
		} catch (Exception e) {
			e.printStackTrace();
			usmap.put(String.valueOf(StatusCode.LACKVALUE), ResultType.INPUT_NULL);
			return usmap;
		}
	}
	
	/**
	 * 
	 * Description:发货单删除
	 * @param request
	 * @param response
	 * @param id
	 * @author 陈银龙
	 * @date 2017年8月31日下午4:02:11
	 */
	@RequestMapping(value = "/deleteDelOrder", method = RequestMethod.POST)
	@ResponseBody
	public void deleteDelOrder(HttpServletRequest request, HttpServletResponse response,String ids, Long facId){
		try{
			if(null == ids || 0 == ids.length()){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			Long[] id = LongUtils.convert(ids.split("[, ]+"));
			Map<String, Object> usmap = new HashMap<String, Object>();
			usmap.put("ids", id);
			usmap.put("facId", facId);
			Integer flag = orderDeliveryService.deleteDelOrder(usmap);
			if(flag > 0 ){
				doResult(response, true, StatusCode.REMOVESUCCESS, flag, ResultType.REMOVE_SUCCESS);
			}else{
				doResult(response, false, StatusCode.REMOVEERROR, flag, "删除失败,发货单已签收");
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.REMOVE_FAULT);
		}
	}
	
	/**
	 * Description:发货单详情
	 * @param request
	 * @param response
	 * @param OrderId
	 * @param OrderFlag
	 * @author 陈银龙
	 * @date 2017年8月23日下午12:40:18
	 */
	@RequestMapping(value = "/findDeliveryDatailsById", method = RequestMethod.POST)
	@ResponseBody
	public OrderDeliveryInfoEntity findDeliveryDatailsById(HttpServletRequest request, HttpServletResponse response, Long id, Long facId){
		try {
			if(id == null || id < 0 || facId == null || facId < 0){
				return null;
			}
			Map<String, Object> usmap = new HashMap<String, Object>();
			usmap.put("facId",facId);
			usmap.put("id", id);
			OrderDeliveryInfoEntity oe = orderDeliveryService.findDeliveryDatailsById(usmap);
			return oe;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
